package com.dhecp.project.system.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysUserAndRoleJurisdiction;
import com.dhecp.project.system.domain.SysUserAndRoleJurisdictionForShiro;
import com.dhecp.project.system.mapper.SysUserAndRoleJurisdictionMapper;
import com.dhecp.project.system.service.SysUserAndRoleJurisdictionService;

/**
 * 用户及角色权限 服务层
 * @author DH-Xxj
 * @version 2019年10月28日
 */
@Service
public class SysUserAndRoleJurisdictionServiceImpl implements SysUserAndRoleJurisdictionService {

    private static final Logger log = LoggerFactory.getLogger(SysUserAndRoleJurisdictionServiceImpl.class);
    
    @Autowired
    private SysUserAndRoleJurisdictionMapper sysUserAndRoleJurisdictionMapper;

    /**
     * 查询指定用户功能权限shiro标识  功能权限控制
	 * @param userId 必要参数：当前登录用户编号
	 * @param mandator 必要参数：委托（被代理）人
	 * @return HashSet-String
     * @author DH-Xxj
     */
    @Override
    public Set<String> queryJurisdictionForShiroToString(Map<String,Object> params){
    	Set<String> menus = new HashSet<String>();   	
    	List<SysUserAndRoleJurisdictionForShiro> jurisdictionForShiroList =  sysUserAndRoleJurisdictionMapper.findJurisdictionForShiro(params);
    	for (SysUserAndRoleJurisdictionForShiro jurisdictionForShiro : jurisdictionForShiroList){
        	if (!"".equals(jurisdictionForShiro.getShiroSignList())){menus.add(jurisdictionForShiro.getShiroSignList());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignView())){menus.add(jurisdictionForShiro.getShiroSignView());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignAdd())){menus.add(jurisdictionForShiro.getShiroSignAdd());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignEdit())){menus.add(jurisdictionForShiro.getShiroSignEdit());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignDelete())){menus.add(jurisdictionForShiro.getShiroSignDelete());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignCancel())){menus.add(jurisdictionForShiro.getShiroSignCancel());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignFlow())){menus.add(jurisdictionForShiro.getShiroSignFlow());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignImport())){menus.add(jurisdictionForShiro.getShiroSignImport());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignExport())){menus.add(jurisdictionForShiro.getShiroSignExport());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignPrint())){menus.add(jurisdictionForShiro.getShiroSignPrint());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignFileUp())){menus.add(jurisdictionForShiro.getShiroSignFileUp());}
        	if (!"".equals(jurisdictionForShiro.getShiroSignFileDown())){menus.add(jurisdictionForShiro.getShiroSignFileDown());}
        }
    	return menus;
    }

    /**
     * 查询指定用户功能权限shiro标识  功能权限控制
	 * @param userId 必要参数：当前登录用户编号
	 * @param mandator 必要参数：委托（被代理）人
	 * @return List-SysUserAndRoleJurisdictionForShiro
     * @author DH-Xxj
     */
    @Override
    public List<SysUserAndRoleJurisdictionForShiro> queryJurisdictionForShiro(Map<String,Object> params){
    	return sysUserAndRoleJurisdictionMapper.findJurisdictionForShiro(params);
    }
    
	/**
	 * 查询指定角色权限列表
	 * @param pid 必要参数：角色 guid号
	 * @param menuGuid 或 menuPguid 所属菜单或所属菜单上级 guid 二选一
	 * @return List-SysUserAndRoleJurisdiction
	 * @author DH-Xxj
	 */
    @Override
    public List<SysUserAndRoleJurisdiction> queryUserAndRoleJurisdictionListForRole(Map<String, Object> params){
    	params.put("userLevel", ShiroUtils.getMandatorLevel());
    	PageUtils.startPage();
    	List<SysUserAndRoleJurisdiction> sysUserAndRoleJurisdictionList = sysUserAndRoleJurisdictionMapper.findJurisdictionListForRole(params);
		return sysUserAndRoleJurisdictionList;    	
    }

	/**
	 * 查询指定用户权限列表
	 * @param pid 必要参数：用户编号 userId
	 * @param searchModule 或 menuGuid 或 menuPguid 具体功能或所属菜单或所属菜单上级 guid 三选一
	 * @return List-SysUserAndRoleJurisdiction
	 * @author DH-Xxj
	 */
    @Override
    public List<SysUserAndRoleJurisdiction> queryUserAndRoleJurisdictionListForUser(Map<String, Object> params){
    	params.put("userLevel", ShiroUtils.getMandatorLevel());
    	PageUtils.startPage();
    	List<SysUserAndRoleJurisdiction> sysUserAndRoleJurisdictionList = sysUserAndRoleJurisdictionMapper.findJurisdictionListForUser(params);
		return sysUserAndRoleJurisdictionList;    	
    }
    
	/**
	 * 设置用户及角色权限
	 * @param infoItems 必要参数：用户及角色权限表对应实体类的json格式字符串
	 * @param upType 必要参数：权限修改类型（formodule：功能权限，fordata：数据权限）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult updateUserAndRoleJurisdiction(Map<String, Object> params){
        List<SysUserAndRoleJurisdiction> sysUserAndRoleJurisdictionList = JSON.parseArray(params.get("infoItems").toString(), SysUserAndRoleJurisdiction.class);
        
        if (sysUserAndRoleJurisdictionList == null || sysUserAndRoleJurisdictionList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }

        if (params.get("upType") == null || params.get("upType") == ""){
        	return AjaxResult.error("请传入权限修改类型（formodule：功能权限，fordata：数据权限）");
        }else if ("formodule".equals(params.get("upType").toString().toLowerCase())){            
            for (SysUserAndRoleJurisdiction sysUserAndRoleJurisdiction : sysUserAndRoleJurisdictionList) {
                if (sysUserAndRoleJurisdiction != null){
                    if (sysUserAndRoleJurisdictionMapper.findJurisdiction(sysUserAndRoleJurisdiction) != null){
                    	sysUserAndRoleJurisdiction.setUpdateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setUpdateTime(new Date());
                		sysUserAndRoleJurisdictionMapper.updateForModule(sysUserAndRoleJurisdiction);
                    }else{
                    	sysUserAndRoleJurisdiction.setUpdateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setUpdateTime(new Date());
                    	sysUserAndRoleJurisdiction.setCreateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setCreateTime(new Date());
                    	sysUserAndRoleJurisdictionMapper.insert(sysUserAndRoleJurisdiction);
                    }
                }
            }
        }else if ("fordata".equals(params.get("upType").toString().toLowerCase())){            
            for (SysUserAndRoleJurisdiction sysUserAndRoleJurisdiction : sysUserAndRoleJurisdictionList) {
                if (sysUserAndRoleJurisdiction != null){
                    if (sysUserAndRoleJurisdictionMapper.findJurisdiction(sysUserAndRoleJurisdiction) != null){
                    	sysUserAndRoleJurisdiction.setUpdateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setUpdateTime(new Date());
                		sysUserAndRoleJurisdictionMapper.updateForData(sysUserAndRoleJurisdiction);
                    }else{
                    	sysUserAndRoleJurisdiction.setUpdateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setUpdateTime(new Date());
                    	sysUserAndRoleJurisdiction.setCreateUser(ShiroUtils.getUserId());
                    	sysUserAndRoleJurisdiction.setCreateTime(new Date());
                    	sysUserAndRoleJurisdictionMapper.insert(sysUserAndRoleJurisdiction);
                    }
                }
            }
        }else{
        	return AjaxResult.error("无效的权限修改类型！");
        }
        
    	return AjaxResult.success("操作成功");
    }
    
}
